<template>
  <div class="roleAdd">
    <el-form :model="message" ref="message" :rules="rules" label-width="80px">
        <el-form-item label="类名称" prop="className">
            <el-input ref="className" v-model.trim="message.className" autofocus></el-input>
        </el-form-item>
        <el-form-item label="实例名称" prop="instanceName">
            <el-input ref="instanceName" v-model.trim="message.instanceName"></el-input>
        </el-form-item>
        <el-form-item label="字典标识" prop="dicMark">
            <el-input ref="dicMark" v-model.trim="message.dicMark"></el-input>
        </el-form-item>
        <el-form-item label="字典名称" prop="dicName">
            <el-input ref="dicName" v-model.trim="message.dicName"></el-input>
        </el-form-item>
        <el-form-item label="字典值" prop="dicVal">
            <el-input ref="dicVal" v-model.trim="message.dicVal"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" class="save" @click="save('message',message.dicBh)">保存</el-button>
            <el-button class="quxiao" @click="closeLayer">取消</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "roleAdd",
  props: ["message"],
  data() {
    return {
      rules: {
        className: [
          { required: true, message: "请填写类名称", trigger: "blur" },
          { min: 0, max: 50, message: "最大长度50", trigger: "blur" }
        ],
        instanceName: [
          { required: true, message: "请填写实例名称", trigger: "blur" },
          { min: 0, max: 50, message: "最大长度50", trigger: "blur" }
        ],
        dicMark: [
          { required: true, message: "请填写字典标识", trigger: "blur" },
          { min: 0, max: 20, message: "最大长度20", trigger: "blur" }
        ],
        dicName: [
          { required: true, message: "请填写字典名", trigger: "blur" },
          { min: 0, max: 50, message: "最大长度50", trigger: "blur" }
        ],
        dicVal: [
          { required: true, message: "请填写字典值", trigger: "blur" },
          { min: 0, max: 10, message: "最大长度10", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //关闭layer弹窗
    closeLayer() {
      layer.closeAll();
    },
    save(formName, dicBh) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 验证通过，在这里写要执行的方法
          axios
            .put(
              dictionariesURL + "/" + dicBh,
              {
                className: this.message.className,
                instanceName: this.message.instanceName,
                dicMark: this.message.dicMark,
                dicName: this.message.dicName,
                dicVal: this.message.dicVal
              },
              {
                headers: { Authorization: appDB.get("userInfo").token }
              }
            )
            .then(res => {
              let d = res.data;
              if (d.resultCode == 0) {
                this.closeLayer(); //关闭弹窗
                this.$parent.getDictionary(); //添加成功以后重新渲染列表获得最新数据
              } else {
                layer.msg(d.msg);
              }
            })
            .catch(error => {
              layer.msg("网络错误");
            });
        } else {
          // 验证未通过，在这里写要执行的方法
          // console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.roleAdd {
  width: 80%;
  padding: 40px 10%;
  float: left;
}
.save {
  padding: 8px 40px;
  background: #38b824;
  border-color: #38b824;
  border-radius: 30px;
  float: right;
  margin-left: 30px;
}
.quxiao {
  border: 0;
  background: #cecece;
  padding: 8px 40px;
  border-color: #367fb6;
  border-radius: 30px;
  float: right;
}
</style>


